import PAGE from '../../../utils/middleware/page'
import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import store from '../../../store/index'
const app = getApp()
Page(PAGE({
    behaviors: [storeBindingsBehavior],
    storeBindings: {
        store,
        fields: {
            needRefresh: 'vehicle.needRefresh',
            needNavigate: 'vehicle.needNavigate',
            needNavigatePage: 'vehicle.needNavigatePage'
        },
        actions: {
            updateNeedRefresh: 'vehicle.updateNeedRefresh',
            updateNeedNavigate: 'vehicle.updateNeedNavigate',
            updateNeedNavigatePage: 'vehicle.updateNeedNavigatePage'
        }
    },
    data: {
        info: null,
        apiField: {
            module: 'vehicle',
            api: 'MonthPriceInfo',
            data: 'info',
            search: {
                id: null
            }
        },
        selectedMonth: null,
        money: 0,
        type: null,
    },
    onLoad(options) {
        if (options[0]) {
            this.data.apiField.search.id = options[0]
        }
        if (options[1]) {
            this.data.type = options[1]
        }
        this.fetchData()
    },
    async fetchData() {
        this.handleFetchData({
            complete: (data) => {
                this.setData({
                    selectedMonth: data.PriceItems[0]
                })
                this.calc()
            }
        })
    },
    handleMonthChange(e) {
        var item = e.currentTarget.dataset.item
        this.setData({
            selectedMonth: item,
        })
        this.calc()
    },
    calc() {
        var money = this.data.selectedMonth.Price
        if (this.data.info.CardDeposit > 0) {
            money += this.data.info.CardDeposit
        }
        this.setData({
            money: money
        })
    },
    async handlePay(e) {
        try {
            const request = {
                VehicleId: this.data.apiField.search.id,
                Month: this.data.selectedMonth.Month,
                TotalMoney: this.data.money
            }

            const res = await wx.$Api.vehicle.Pay({ data: request })
            const prePayRes = await wx.$Api.payment.PrePay({ data: { tradeNo: res.Data } })
            console.log('支付参数', prePayRes)
            wx.$Fun.wxPay(prePayRes.Data).then(resp => {
                this.updateNeedRefresh(true)
                this.back()
            }).catch(e => {
                console.log(e)
            })
        }
        catch (e) {
            wx.$Fun.showToast(e.Message)
        }
    },
    back() {
        if (this.data.type == 1) {
            //返回代缴费页面，并跳转到套餐列表页面
            this.updateNeedNavigate(true)
            this.updateNeedNavigatePage('vehicleList')
            wx.navigateBack({
                delta: 3
            })
        }
        else if (this.data.type == 2) {
            //返回到首页并跳转到套餐列表页面
            this.updateNeedNavigate(true)
            this.updateNeedNavigatePage('vehicleList')
            wx.navigateBack({
                delta: 4
            })
        }
        else {
            //返回上一页
            this.updateNeedRefresh(true)
            wx.navigateBack({
                delta: 1
            })
        }
    }
}))